<?php

class ProductController extends Controller
{

    public function actionIndex()
    {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $product_url = Yii::app()->getRequest()->getParam('url');
        $product_object = Products::model()
            ->cache(31536000)
            ->find(array(
                'condition' => 'url=:url',
                'params' => array(':url' => $product_url),
            ));
        //TODO: redirect if product object not found
        $all_models = Models::model()
            ->cache(31536000)
            ->findAll();
        $product_cat_model = ProductCatModel::model()
            ->cache(2592000)
            ->findAll(array(
                'condition' => 'product_asin=:product_asin',
                'params' => array(':product_asin' => $product_object->asin),
            ));
        $product_offers = ProductOffer::model()
            ->cache(604800)
            ->findAll(array(
                'condition' => 'product_asin=:product_asin',
                'params' => array(':product_asin' => $product_object->asin),
            ));
        $product_serialize = ProductSerialize::model()
            ->cache(604800)
            ->find(array(
                'condition' => 'product_asin=:product_asin',
                'params' => array(':product_asin' => $product_object->asin),
            ));

        $FitTablesHtml = $this->generateFitTablesHtml($product_object->asin, $all_models, $product_cat_model);
        $OfferTableHtml = $this->generateOfferTableHtml($product_object->asin, $product_offers);
        $this->render(
            'index',
            array(
                "FitTablesHtml" => $FitTablesHtml,
                "OfferTableHtml" => $OfferTableHtml,
                "product_object" => $product_object,
                "product_serialize" => unserialize($product_serialize->serialize),
            )
        );
    }

    public function generateFitTablesHtml($product_asin, $all_models, $product_cat_model)
    {
        $html = "";
        $model_years = range(2010, 2015);
        foreach ($model_years as $year) {
            $html .= "<div id=\"tabs-{$year}\"><table class=\"table table-striped\"><tbody>";
            foreach ($all_models as $model) {
                if ($model->year == $year) {
                    $html .= "<tr>";
                    $fit = false;
                    foreach ($product_cat_model as $pcm) {
                        if ($pcm->product_asin == $product_asin && $pcm->model_id == $model->id) {
                            $fit = true;
                            break;
                        }
                    }
                    if ($fit == true) {
                        $html .= "<td><i class=\"fa fa-check-square-o\"></i></td>";
                    } else {
                        $html .= "<td><i class=\"fa fa-times\"></i></td>";
                    }
                    $html .= "<td>" . $model->year . "</td>";
                    $html .= "<td>" . $model->att3 . "</td>";
                    $html .= "<td>" . $model->att1 . "</td>";
                    $html .= "<td>" . $model->att2 . "</td>";
                    if ($fit == true) {
                        $link = "http://www.amazon.com/dp/{$product_asin}/?vehicle={$model->path}&tag=bestchoiceever-20";
                        $html .= "<td><a rel=\"nofollow\" target=\"_blank\" href=\"{$link}\"><i class=\"fa fa-external-link\"></i></a></td>";
                    } else {
                        $html .= "<td></td>";
                    }
                    $html .= "</tr>";
                }
            }
            $html .= "</tbody></table></div>";
        }
        return $html;
    }

    public function generateOfferTableHtml($product_asin, $product_offers)
    {
        $html = "<table class=\"table table-bordered table-striped table-hover table-heading table-datatable\" id=\"offer-table\">";
        $html .= "<thead><tr><th style=\"text-align:center\">Seller</th><th style=\"text-align:center\">Price</th><th style=\"text-align:center\">Condition</th><th style=\"text-align:center\">Shipping</th><th style=\"text-align:center\">Location</th></tr></thead><tbody>";
        foreach ($product_offers as $offer) {
            $html .= "<tr>";
            $link = "http://www.amazon.com/dp/{$product_asin}/?m={$offer->seller_id}&s=merchant-items&tag=bestchoiceever-20";
            if ($offer->seller_name != "") {
                $html .= "<td style=\"width:200px;text-align:center\"><a rel=\"nofollow\" target=\"_blank\" href=\"{$link}\">" . $offer->seller_name . "</a></td>";
            } else {
                $html .= "<td style=\"width:200px;text-align:center\"><a rel=\"nofollow\" target=\"_blank\" href=\"{$link}\"><img style=\"width:120px\" src =\"" . $offer->seller_img . "\" /></a></td>";
            }

            $ship_price = $offer->shipping != '0' ? $offer->shipping : "Free <a title=\"Get info\" rel=\"nofollow\" target=\"_blank\" href=\"{$link}\"><i class=\"fa fa-info-circle\"></i></a>";
            $html .= "<td style=\"text-align:center\">" . $offer->price_formatted . "</td>";
            $html .= "<td style=\"text-align:center\">" . $offer->condition . "</td>";
            $html .= "<td style=\"text-align:center\">" . $ship_price . "</td>";
            $html .= "<td style=\"text-align:center\">" . $offer->ship_from . "</td>";
            $html .= "</tr>";
        }
        $html .= "</tbody></table>";
        return $html;
    }
}